﻿using Microsoft.Phone.Info;
using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;

namespace WhallaToolkit.DeviceInfoService
{
    public class DeviceInfoService : IDeviceInfoService
    {
        #region Interface
        public async Task<DeviceInfo> GetDeviceInfo()
        {
            DeviceInfo info = new DeviceInfo()
            {
                ApplicationVersion = GetPackageVersion(),
                DeviceCountry = GetDeviceRegion(),
                DeviceId = GetDeviceID(),
                DeviceName = GetDeviceName(),
                OsVersion = GetOsVersion()
            };

            return info;
        }
        #endregion

        #region Private methods
        private string GetDeviceName()
        {
            try
            {
                string result = Microsoft.Phone.Info.DeviceStatus.DeviceName;

                object manufacterName;
                DeviceExtendedProperties.TryGetValue("DeviceManufacturer", out manufacterName);

                if (manufacterName != null)
                {
                    result = string.Format("{0}-{1}", manufacterName, result);
                }

                return result;
            }
            catch (Exception)
            {

                return string.Empty;
            }
           
        }

        private string GetDeviceID()
        {
            try
            {
                byte[] myDeviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
                string result = "WinPhone:" + BitConverter.ToString(myDeviceID).Replace("-", string.Empty);

                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
            
        }

        private string GetDeviceRegion()
        {
            return System.Globalization.CultureInfo.CurrentCulture.EnglishName;
        }

        private string GetPackageVersion()
        {
            XDocument xDocument = XDocument.Load("WMAppManifest.xml");
            XmlReader manifestReader = xDocument.CreateReader(ReaderOptions.None);
            using (manifestReader)
            {
                manifestReader.ReadToDescendant("App");
                if (!manifestReader.IsStartElement())
                {
                    throw new FormatException("Wrong tag - not found in WMAppManifest.xml ");
                }
                manifestReader.MoveToFirstAttribute();

                while (manifestReader.MoveToNextAttribute())
                {
                    if (manifestReader.Name == "Version")
                        return manifestReader.Value;
                }
            }

            return "N.A.N";
        }

        private string GetOsVersion()
        {
            //string result = string.Format("{0}", Environment.OSVersion.ToString());
            string result = string.Format("{0}.{1}.{2}.{3}", Environment.OSVersion.Version.Major, Environment.OSVersion.Version.Minor, Environment.OSVersion.Version.Build, Environment.OSVersion.Version.Revision);
            return result;
        }

        private string GetProcesorArchitecture()
        {
            return "ARM";
        }


        #endregion
    }
}
